
package com.cyys.common.ql;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * $.ajax后需要接受的JSON
 * 
 * @author
 * 
 */
@Deprecated
public abstract class BaseReturn implements java.io.Serializable{
	/**
	 * @description: 返回错误信息
	 * @param msg 失败提示信息
	 * @return ${return_type}
	 * @throws
	 * @author lishengquan
	 * @date 2019/6/10 10:21
	 * @version 1.0
	 */
	protected AjaxJson error(String msg) {
		AjaxJson aj = new AjaxJson();
		aj.setSuccess(false);
		aj.setMsg(msg);
		return aj;
	}

	/**
	 * @description: 数据返回
	 * @param list
	 * @return Map
	 * @throws
	 * @author lishengquan
	 * @date 2019/6/10 10:11
	 * @version 1.0
	 */
	protected AjaxJson successList(List<?> list) {
		AjaxJson ajaxJson = new AjaxJson();
		LinkedHashMap<String, Object> data = new LinkedHashMap<>(16);
		data.put("data",list);
		ajaxJson.setSuccess(true);
		ajaxJson.setMsg("操作成功");
		ajaxJson.setData(data);
		return ajaxJson;
	}

	/**
	 * 加密数据返回分页数据
	 * @description: 成功返回的数据结果集
	 * @param page  封装的结果集
	 * @return Map
	 * @throws
	 * @author lishengquan
	 * @date 2019/6/10 10:11
	 * @version 1.0
	 */
	public AjaxJson successIPage(IPage page) {
		AjaxJson ajaxJson = new AjaxJson();
		LinkedHashMap<String, Object> data = new LinkedHashMap<>(16);
		Long pageSize = page.getSize();
		Long totalCount = page.getTotal();
		data.put("data",page.getRecords());
		data.put("pageSize", pageSize);
		data.put("pageNum",page.getCurrent());
		data.put("totalCount",totalCount);
		Long totalPage = null;
		if ( totalCount != null && pageSize != null && pageSize > 0) {
			totalPage = (totalCount + pageSize - 1) / pageSize;
		}
		data.put("totalPage", totalPage);
		ajaxJson.setData(data);
		ajaxJson.setSuccess(true);
		ajaxJson.setMsg("操作成功");
		return ajaxJson;
	}

	public AjaxJson successObject(Object object) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.put("data",object);
		ajaxJson.setSuccess(true);
		ajaxJson.setMsg("操作成功");
		return ajaxJson;
	}

	public AjaxJson successObject(Object object, String msg) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.put("data",object);
		ajaxJson.setSuccess(true);
		ajaxJson.setMsg(msg);
		return ajaxJson;
	}

	/**
	 * @description: 成功返回提示信息
	 * @param msg 成功提示信息
	 * @return ${return_type}
	 * @throws
	 * @author lishengquan
	 * @date6/10 10:21
	 * @version 1.0
	 */
	protected AjaxJson success(String msg) {
		AjaxJson aj = new AjaxJson();
		aj.setSuccess(true);
		aj.setMsg(msg);
		return aj;
	}
}
